//
//  ExercisesQuqestionChoiceItemCell.m
//  ShowYork
//
//  Created by hxh on 2020/9/8.
//  Copyright © 2020 hxh. All rights reserved.
//

#import "ExercisesQuqestionChoiceItemCell.h"

@interface ExercisesQuqestionChoiceItemCell ()
@property (nonatomic,strong)UIView *containView;
@property (nonatomic,strong)UIView *choiceView;
@property (nonatomic,strong)UIView *line;
@property (nonatomic,strong)UILabel *contentLb;
@property (nonatomic,strong)UILabel *choiceLb;

@property (nonatomic,strong)UIButton *coverBtn;

@property (nonatomic,strong)UIImageView *correctImgView;
@property (nonatomic,strong)UIImageView *errorImgView;
@end

@implementation ExercisesQuqestionChoiceItemCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setUI];
    }
    return self;
}

- (void)setUI{
    self.containView = [[UIView alloc]init];
    self.containView.layer.cornerRadius = 14;
    self.containView.layer.borderWidth = 1;
    self.containView.layer.borderColor = RGBA(221, 221, 221, 1).CGColor;
    self.containView.layer.masksToBounds = YES;
    [self.contentView addSubview:self.containView];
    
    [self.containView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(16);
        make.right.equalTo(self.contentView).offset(-16);
        make.top.equalTo(self.contentView);
        make.bottom.equalTo(self.contentView).offset(-16);
    }];
    
    self.choiceView = [[UIView alloc]init];
    [self.containView addSubview:self.choiceView];
    [self.choiceView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.bottom.equalTo(self.containView);
        make.width.mas_equalTo(50);
    }];
    
    self.line = [[UIView alloc]init];
    self.line.backgroundColor = RGBA(221, 221, 221, 1);
    [self.containView addSubview:self.line];
    
    [self.line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.containView);
        make.size.mas_equalTo(CGSizeMake(1, 20));
        make.left.equalTo(self.choiceView.mas_right);
    }];
    
    self.contentLb = [[UILabel alloc]init];
//    self.contentLb.text = @"如果心灵的窗户被“关”上了，或者蒙了尘，这些近视的学生，不仅无法更好地拥有出彩人生，也看不清美丽未来";
    self.contentLb.textColor = RGBA(102, 102, 102, 1);
    self.contentLb.font = font(14);
    self.contentLb.numberOfLines = 0;
    [self.containView addSubview:self.contentLb];
    
    [self.contentLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.choiceView.mas_right).offset(16);
        make.right.equalTo(self.containView).offset(-16);
        make.top.equalTo(self.containView).offset(16);
        make.bottom.equalTo(self.containView).offset(-16);
    }];
    
    self.choiceLb = [[UILabel alloc]init];
    self.choiceLb.textColor = RGBA(35, 24, 21, 1);
    self.choiceLb.font = [UIFont boldSystemFontOfSize:18];
    [self.containView addSubview:self.choiceLb];
    [self.choiceLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.choiceView);
    }];
    
    self.coverBtn = [[UIButton alloc]init];
    [self.coverBtn addTarget:self action:@selector(coverMethod) forControlEvents:UIControlEventTouchUpInside];
    [self.containView addSubview:self.coverBtn];
    
    [self.coverBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(self.containView);
    }];
    
    self.correctImgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"exercises_tag_correct"]];
    self.correctImgView.hidden = YES;
    [self.contentView addSubview:self.correctImgView];
    
    [self.correctImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(self.choiceLb);
    }];
    
    self.errorImgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"exercises_tag_x"]];
    self.errorImgView.hidden = YES;
    [self.contentView addSubview:self.errorImgView];
    
    
     [self.errorImgView mas_makeConstraints:^(MASConstraintMaker *make) {
         make.center.mas_equalTo(self.choiceLb);
     }];
}

+(instancetype)cellWithTableView:(UITableView *)tableView{
    NSString *ID = @"ExercisesQuqestionChoiceItemCell";
    ExercisesQuqestionChoiceItemCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[ExercisesQuqestionChoiceItemCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return cell;
}



- (void)setModel:(ExercisesQuestionChoiceItemModel *)model{
    _model = model;
  
    self.coverBtn.backgroundColor = model.isSelected?RGBA(86, 128, 250, 0.1):[UIColor clearColor];
    self.containView.layer.borderColor = model.isSelected?[RPAppManager themColor].CGColor:RGBA(221, 221, 221, 1).CGColor;
    
    self.choiceLb.text = model.prefix;
    
    [[HTMLHelper alloc]attributedTextFromHTML:model.content andCompleteBlock:^(NSMutableAttributedString *result) {
        self.contentLb.attributedText = result;
        if (self.delegate && [self.delegate respondsToSelector:@selector(updateTableViewFrame)]) {
            [self.delegate updateTableViewFrame];
        }
    }];
}

- (void)coverMethod{
    
    if (!_model.isSelected) {
        _model.isSelected = YES;
        self.coverBtn.backgroundColor = RGBA(86, 128, 250, 0.1);
        self.containView.layer.borderColor = [RPAppManager themColor].CGColor;
    }
    if (self.delegate && [self.delegate respondsToSelector:@selector(selectedChoiceWithModel:)]) {
        [self.delegate selectedChoiceWithModel:_model];
    }
}

- (void)setQuqestionChoiceModel:(ExercisesQuestionChoiceItemModel *)choiceModel answerModel:(ExercisesAnswerItemModel *)answerModel correntModel:(ExercieseQuestionItemModel *)correctModel{
    _model = choiceModel;

    [[HTMLHelper alloc]attributedTextFromHTML:choiceModel.content andCompleteBlock:^(NSMutableAttributedString *result) {
        self.contentLb.attributedText = result;
        if (self.delegate && [self.delegate respondsToSelector:@selector(updateTableViewFrame)]) {
            [self.delegate updateTableViewFrame];
        }
    }];
    
    self.choiceLb.text = choiceModel.prefix;
    
    self.userInteractionEnabled = answerModel == nil;
    
    if (answerModel != nil) {
        if (answerModel.content.length == 0) {
            if ([correctModel.correct isEqualToString:choiceModel.prefix]) {
                self.containView.layer.borderColor = RGBA(109, 221, 127, 1).CGColor;
                self.choiceView.backgroundColor = RGBA(109, 221, 127, 1);
                self.choiceLb.text = @"";
                self.errorImgView.hidden = YES;
                self.correctImgView.hidden = NO;
            }else{
                self.errorImgView.hidden = YES;
                self.correctImgView.hidden = YES;
                self.choiceView.backgroundColor = [UIColor whiteColor];
                self.containView.layer.borderColor = RGBA(221, 221, 221, 1).CGColor;
            }
        }else{
            // 首先判断当前项，回答项，正确答案是否是同一项
            if ([correctModel.correct isEqualToString:answerModel.content] && [choiceModel.prefix isEqualToString:answerModel.content]) {
                self.containView.layer.borderColor = RGBA(109, 221, 127, 1).CGColor;
                self.choiceView.backgroundColor = RGBA(109, 221, 127, 1);
                self.choiceLb.text = @"";
                self.errorImgView.hidden = YES;
                self.correctImgView.hidden = NO;
                self.line.hidden = YES;
            }else{
                if ([choiceModel.prefix isEqualToString:correctModel.correct]) {
                    self.containView.layer.borderColor = RGBA(109, 221, 127, 1).CGColor;
                    self.choiceView.backgroundColor = RGBA(109, 221, 127, 1);
                    self.choiceLb.text = @"";
                    self.errorImgView.hidden = YES;
                    self.correctImgView.hidden = NO;
                    self.line.hidden = YES;
                }else{
                    if ([answerModel.content isEqualToString:choiceModel.prefix]) {
                        self.containView.layer.borderColor = RGBA(255, 100, 99, 1).CGColor;
                        self.choiceView.backgroundColor = RGBA(255, 100, 99, 1);
                        self.choiceLb.text = @"";
                        self.errorImgView.hidden = NO;
                        self.correctImgView.hidden = YES;
                        self.line.hidden = YES;
                    }else{
                        self.errorImgView.hidden = YES;
                        self.correctImgView.hidden = YES;
                        self.choiceView.backgroundColor = [UIColor whiteColor];
                        self.containView.layer.borderColor = RGBA(221, 221, 221, 1).CGColor;
                        self.line.hidden = NO;
                    }
                }
            }
        }
    }else{
        self.coverBtn.backgroundColor = choiceModel.isSelected?RGBA(86, 128, 250, 0.1):[UIColor clearColor];
        self.containView.layer.borderColor = choiceModel.isSelected?[RPAppManager themColor].CGColor:RGBA(221, 221, 221, 1).CGColor;
        self.errorImgView.hidden = YES;
        self.correctImgView.hidden = YES;
    }
}
@end
